package com.superheroes.controller.open.excle;

import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import com.alibaba.fastjson.JSON;
import com.superheroes.utils.result.NotSayUncle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("import")
@Api(tags = "导入测试")
public class ImportTestController {


    private void writeResponse(HttpServletResponse response, Object obj) throws IOException {
        response.setContentType("application/json; charset=UTF-8");
        response.getOutputStream().write(JSON.toJSONString(obj).getBytes());
        response.getOutputStream().close();
    }


    @ApiOperation(value = "导入接口")
    @PostMapping("test")
    public NotSayUncle test(HttpServletResponse response, @RequestParam("file") MultipartFile file) throws Exception {

        // Excel数据
        InputStream inputStream = file.getInputStream();
//        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
//        List<aaaa> all = reader.readAll(aaaa.class);


         ExcelUtil.readBySax(file.getInputStream(), 0, createRowHandler());
        return  NotSayUncle.ok("");
    }


    private RowHandler createRowHandler() {
        return new RowHandler() {
            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> list) {
                Console.log("[{}] [{}] {}", sheetIndex, rowIndex, list);
            }
        };
    }


}